<?php
namespace Api\Controller;
use Think\Controller;
class UserController extends CommonController {
	public function _initialize()
	{
		parent::_initialize();
	}
	
	public function login()
    {
		$user		= I('get.user');
		$password	= I('get.password');
		//调出数据库查询账号
		$User = M('User');
		$password = md5($password);
		$list = $User->where("email='{$user}' and password='{$password}'")->find();
		
		if( $list == false )
		{
			$this->response( false , 1 , '用户名或者密码不正确' , true);
		}
		$_SESSION['uid']  = $list['id'];
		$_SESSION['name'] = $list['email'];
		$this->response( true , 0 , '登录成功' , true);
	}
	
    public function register()
    {
        $email 			= I('get.email');
        $password		= I('get.password');
        $passwordRepeat	= I('get.passwordRepeat');//接收js数据
        
        $User = M('User');//导入数据库
        //查询是否存在账号
        $list = $User->where("email={$email}")->find();
        
        if($list == true){
			$this->response(false , 1 , '用户名已经存在' , true);
		}
		else
		{
			if( $password != $passwordRepeat)
			{
				$this->response(false , 2 , '两次密码不一致' , true);
			}
			else{
				$data = array(
					'email'		=> $email,
					'password'	=> md5($password),
					'addtime'	=> time(),
				);//添加数据到数据库
				
				$ret = $User->add($data);
				
			}
		}
		$this->response(true , 0 , '注册成功' , true);
    }
    
    public function loginin()
    {
		if( isset($_SESSION['uid']) && $_SESSION['uid'] !='' 
			&& isset($_SESSION['name']) && $_SESSION['name'] !='')
		{
			$data = array(
				'name'	=> $_SESSION['name']
			);	
			$this->response( true , 0 , $data , true);
		}
		$this->response( false , 0 , '未登录' ,true);
	}
	
	public function loginout()
	{
		$_SESSION['uid']	= '' ;
		$_SESSION['name']	= '' ;
		
		$this->response( true , 0 , '已退出' ,true);
	}
}
